package com.kk.controller;

import com.kk.Entry.Employee;
import com.kk.Entry.R;
import com.kk.dto.EmpDto;
import com.kk.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("emps")
public class EmpController {

    @Autowired
    EmpService empService;

    @GetMapping
    public R findPageEmp(EmpDto dto) {
        Map<String, Object> map = empService.findPageEmp(dto);
        if (map != null) {
            return R.success(map);
        }
        return R.fail("查询失败");
    }

    @DeleteMapping("/{ids}")
    public R deleteEmpS(@PathVariable ArrayList<Integer> ids) {
        int i = empService.deleteEmps(ids);
        if (i > 0) {
            return R.success("成功删除" + i + "个员工");
        }
        return R.fail("删除失败0");
    }

    @PostMapping
    public R addEmp(@RequestBody Employee employee) {
       int i = empService.addEmp(employee);
       if (i > 0) {
          return R.success("增加成功");
       }
       return R.fail("失败");
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Integer id) {
        Employee emp = empService.findById(id);
        return R.success(emp);
    }
    @PutMapping
    public R updateEmp(@RequestBody Employee employee) {
       int i = empService.updateEmp(employee);
       return R.success(i);
    }




}
